home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / avril20 / cfg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  10.1 KB  |  353 lines

  1. /* Read an AVRIL-style CFG file */
  2.  
  3. /* Written by Bernie Roehl, July 1994 */
  4.  
  5. /* Copyright 1994 by Bernie Roehl */
  6.  
  7. /* You may use this code for your own non-commercial projects without
  8.    paying any fees or royalties.  "Non-commercial", in this context,
  9.    means that the software you write is given away for free to anyone
  10.    who wants it.
  11.    
  12.    Commercial use, including shareware, requires a licensing
  13.    fee and a specific written agreement with the author.
  14.  
  15.    All programs created using this software (both commercial and
  16.    non-commercial) must acknowledge the use of the AVRIL library,
  17.    both in the documentation and in a banner screen at the start or
  18.    end of the program.
  19.  
  20.    For more information, contact Bernie Roehl (broehl@uwaterloo.ca).
  21.  
  22. */
  23.  
  24. #include "avril.h"
  25. #include "avrildrv.h"
  26. #include <stdlib.h>   /* strtoul(), atof() */
  27. #include <string.h>
  28. #include <ctype.h>
  29.  
  30. /* These next few externs should really be in avrildrv.h */
  31.  
  32. extern vrl_DisplayDriverFunction vrl_DisplayDriverDefault;
  33. extern vrl_DisplayDriverFunction vrl_DisplayDriverModeY;
  34.  
  35. static int show_compass = 0, show_position = 0, show_framerate = 0;
  36.  
  37. static int getline(char *buff, int maxbytes, FILE *in)
  38.     {
  39.     char *p;
  40.     if (fgets(buff, maxbytes, in) == NULL) return 0;
  41.     if ((p = strchr(buff, '\n')) != NULL) *p = '\0';
  42.     return 1;
  43.     }
  44.  
  45. static int lookup(char *token, char *table[], int n)
  46.     {
  47.     int i;
  48.     for (i = 0; i < n; ++i)
  49.         if (!stricmp(token, table[i]))
  50.             return i;
  51.     return -1;
  52.     }
  53.  
  54. static int parse(char *buff, char *seps, char *argv[])
  55.     {
  56.     char *p;
  57.     int argc = 0;
  58.     if ((p = strchr(buff, '#')) != NULL) *p = '\0';
  59.     p = strtok(buff, seps);
  60.     while (p)
  61.         {
  62.         argv[argc++] = p;
  63.         p = strtok(NULL, seps);
  64.         }
  65.     return argc;
  66.     }
  67.  
  68. static char *statement_table[] =
  69.     {
  70.     "version", "loadpath", "include",
  71.     "device", "devconfig", "displaydriver", "videodriver", "sounddriver",
  72.     "compass", "framerate", "position", "cursor",
  73.     "stereoparams", "stereoleft", "stereoright", "stereotype"
  74.     };
  75.  
  76. static enum statements
  77.     {
  78.     st_version, st_loadpath, st_include,
  79.     st_device, st_devconfig, st_displaydriver, st_videodriver, st_sounddriver,
  80.     st_compass, st_framerate, st_position, st_cursor,
  81.     st_stereoparams, st_stereoleft, st_stereoright, st_stereotype,
  82.     ncmds
  83.     };
  84.  
  85. static char *stereotypes[] =
  86.     {
  87.     "NONE", "SEQUENTIAL",
  88.     "ANAGLYPH_SEQUENTIAL", "ANAGLYPH_WIRE_ALTERNATE",
  89.     "ENIGMA", "FRESNEL",
  90.     "CYBERSCOPE", "CRYSTALEYES",
  91.     "CHROMADEPTH", "SIRDS",
  92.     "TWOCARDS", "ANAGLYPH_SOLID_ALTERNATE"
  93.     };
  94.  
  95. static int nstereotypes = 12;
  96.  
  97. static int driver_setup(int argc, char *argv[])
  98.     {
  99.     int mode = 0, serial = 1, buffsize = 2000, irq = 3;
  100.     unsigned int address = 0x2F8;
  101.     char *nickname = "No name device", *drivername = "Unknown device";
  102.     vrl_SerialPort *port = NULL;
  103.     vrl_DeviceDriverFunction *fn;
  104.     vrl_Device *device;
  105.     switch (argc)
  106.         {
  107.         case 1: return -1;    /* bad syntax */
  108.         default:
  109.         case 7: buffsize = strtoul(argv[6], NULL, 0);
  110.         case 6: irq = strtoul(argv[5], NULL, 0);
  111.         case 5: address = strtoul(argv[4], NULL, 0);
  112.         case 4: mode = strtoul(argv[3], NULL, 0);
  113.         case 3: drivername = strdup(argv[2]);
  114.         case 2: nickname = strdup(argv[1]);
  115.         }
  116.     if (!stricmp(drivername, "mouse")) { fn = vrl_MouseDevice; serial = 0; }
  117.     else if (!stricmp(drivername, "GDC")) { fn = vrl_GlobalDevice; serial = 1; }
  118.     else if (!stricmp(drivername, "Cyberman")) { fn = vrl_CybermanDevice; serial = 1; }
  119.     else if (!stricmp(drivername, "Spaceball")) { fn = vrl_SpaceballDevice; serial = 1; }
  120.     else if (!stricmp(drivername, "RedBaron")) { fn = vrl_RedbaronDevice; serial = 1; }
  121.     else if (!stricmp(drivername, "CTM")) { fn = vrl_CTMDevice; serial = 1; }
  122.     else if (!stricmp(drivername, "Isotrak")) { fn = vrl_IsotrakDevice; serial = 1; }
  123.     else if (!stricmp(drivername, "VIO")) { fn = vrl_VIODevice; serial = 1; }
  124. #ifdef VRL_PC_COMPATABLE
  125.     /* these next few only exist on PC's */
  126.     else if (!stricmp(drivername, "keypad")) { fn = vrl_KeypadDevice; serial = 0; }
  127.     else if (!stricmp(drivername, "joystick")) { fn = vrl_JoystickDevice; serial = 0; }
  128.     else if (!stricmp(drivername, "Pad")) { fn = vrl_PadDevice; serial = 0; }
  129.     else if (!stricmp(drivername, "CyberWand")) { fn = vrl_CyberwandDevice; serial = 0; }
  130.     else if (!stricmp(drivername, "7thSense")) { fn = vrl_7thSenseDevice; serial = 0; }
  131. #endif
  132.     /* add other devices in here, as well as making entries in avrildrv.h */
  133.     else return -2;  /* unknown device */
  134.     if (serial)
  135.         {
  136.         port = vrl_SerialOpen(address, irq, buffsize);
  137.         if (port == NULL)
  138.             return -3;  /* couldn't open serial port */
  139.         }
  140.     device = vrl_DeviceOpen(fn, port);
  141.     if (device == NULL)
  142.         return -4;   /* couldn't open device */
  143.     vrl_DeviceSetNickname(device, nickname);
  144.     vrl_DeviceSetMode(device, mode);
  145.     return 0;    
  146.     }
  147.  
  148. int vrl_ReadCFGProcessLine(char *buff)
  149.     {
  150.     char buffcopy[256];
  151.     int argc;
  152.     char *argv[20];
  153.     vrl_StereoConfiguration *conf;
  154.     strcpy(buffcopy, buff);  /* unparsed version */
  155.     argc = parse(buff, " \t,", argv);
  156.     if (argc < 1) return 0;  /* ignore blank lines */
  157.     if (argc < 2) return 0;  /* all statements currently have at least one parameter */
  158.     switch (lookup(argv[0], statement_table, ncmds))
  159.         {
  160.         st_version: break;
  161.         case st_loadpath: vrl_FileSetLoadpath(argv[1]); break;
  162.         case st_include:
  163.             {
  164.             FILE *new = fopen(vrl_FileFixupFilename(argv[1]), "r");
  165.             if (new)
  166.                 {
  167.                 vrl_ReadCFG(new);
  168.                 fclose(new);
  169.                 }
  170.             }
  171.             break;
  172.         case st_device: driver_setup(argc, argv); break;
  173.         case st_cursor: if (stricmp(argv[1], "on")) vrl_VideoCursorHide(); break;
  174.         case st_compass: show_compass = !stricmp(argv[1], "on"); break;
  175.         case st_position: show_position = !stricmp(argv[1], "on"); break;
  176.         case st_framerate: show_framerate = !stricmp(argv[1], "on"); break;
  177.         case st_displaydriver:
  178.             if (argc < 2) break;
  179.             if (!stricmp(argv[1], "ModeY"))
  180.                 vrl_DisplaySetDriver(vrl_DisplayDriverModeY);
  181.             else
  182.                 vrl_DisplaySetDriver(vrl_DisplayDriverDefault);
  183.             vrl_DisplayInit(NULL);
  184.             break;
  185.         case st_videodriver:
  186.             {
  187.             int mode = 0;
  188.             if (argc < 2) break;
  189.             if (argc > 2) mode = strtoul(argv[2], NULL, 0);
  190.             vrl_VideoShutdown();
  191.             if (!stricmp(argv[1], "Mode13"))
  192.                 vrl_VideoSetDriver(vrl_VideoDriverMode13);
  193.             else if (!stricmp(argv[1], "ModeY"))
  194.                 vrl_VideoSetDriver(vrl_VideoDriverModeY);
  195.             else if (!stricmp(argv[1], "7thSense"))
  196.                 vrl_VideoSetDriver(vrl_VideoDriver7thSense);
  197.             vrl_VideoSetup(mode);
  198.             vrl_DisplayInit(NULL);
  199.             vrl_MouseReset();
  200.             }
  201.             break;
  202.         case st_devconfig:
  203.             {
  204.             vrl_Device *device;
  205.             int channel;
  206.             if (argc < 3) break;
  207.             device = vrl_DeviceFind(argv[1]);
  208.             if (device == NULL) break;
  209.             if (isdigit(*argv[2]))
  210.                 channel = atoi(argv[2]);
  211.             else
  212.                 channel = toupper(*argv[2]) - 'X' + ((toupper(argv[2][1]) == 'R') ? 3 : 0);
  213.             if (argc > 3)
  214.                 {
  215.                 if (toupper(*argv[3]) == 'A')
  216.                     vrl_DeviceSetScale(device, channel, float2angle(atof(&argv[3][1])));
  217.                 else
  218.                     vrl_DeviceSetScale(device, channel, float2scalar(atof(argv[3])));
  219.                 }
  220.             if (argc > 4)
  221.                 vrl_DeviceSetDeadzone(device, channel, float2scalar(atof(argv[4])));
  222.             }
  223.             break;
  224.         case st_stereotype:
  225.             conf = vrl_WorldGetStereoConfiguration();
  226.             if (conf == NULL)
  227.                 vrl_WorldSetStereoConfiguration(conf = vrl_StereoCreateConfiguration());
  228.             if (conf && argc > 1)
  229.                 {
  230.                 int n = lookup(argv[1], stereotypes, nstereotypes);
  231.                 if (n >= 0)
  232.                     vrl_StereoSetType(conf, n);
  233.                 vrl_WorldSetStereo(n);  /* NONE (==0) is FALSE, non-zero is TRUE */
  234.                 }
  235.             break;
  236.         case st_stereoparams:
  237.             conf = vrl_WorldGetStereoConfiguration();
  238.             if (conf == NULL)
  239.                 vrl_WorldSetStereoConfiguration(conf = vrl_StereoCreateConfiguration());
  240.             if (conf == NULL)
  241.                 break;
  242.             if (vrl_StereoGetType(conf) == VRL_STEREOTYPE_CHROMADEPTH)
  243.                 {                    
  244.                 if (argc > 2)
  245.                     vrl_StereoSetChromaFar(conf, float2scalar(atof(argv[2])));
  246.                 if (argc > 1)
  247.                     vrl_StereoSetChromaNear(conf, float2scalar(atof(argv[1])));
  248.                 }
  249.             else
  250.                 {
  251.                 if (argc > 2)
  252.                     vrl_StereoSetConvergence(conf, atof(argv[2]));
  253.                 if (argc > 1)
  254.                     vrl_StereoSetEyespacing(conf, atof(argv[1]));
  255.                 }
  256.             break;
  257.         case st_stereoleft:
  258.             conf = vrl_WorldGetStereoConfiguration();
  259.             if (conf == NULL)
  260.                 vrl_WorldSetStereoConfiguration(conf = vrl_StereoCreateConfiguration());
  261.             if (conf && argc > 2)
  262.                 vrl_StereoSetLeftEyeRotation(conf, float2angle(atof(argv[2])));
  263.             if (conf && argc > 1)
  264.                 vrl_StereoSetLeftEyeShift(conf, float2angle(atof(argv[1])));
  265.             break;
  266.         case st_stereoright:
  267.             conf = vrl_WorldGetStereoConfiguration();
  268.             if (conf == NULL)
  269.                 vrl_WorldSetStereoConfiguration(conf = vrl_StereoCreateConfiguration());
  270.             if (conf && argc > 2)
  271.                 vrl_StereoSetRightEyeRotation(conf, float2angle(atof(argv[2])));
  272.             if (conf && argc > 1)
  273.                 vrl_StereoSetRightEyeShift(conf, float2angle(atof(argv[1])));
  274.             break;
  275.         default:
  276.             vrl_ReadWLDfeature(argc, argv, buffcopy);
  277.             break;
  278.         }
  279.     return 0;
  280.     }
  281.  
  282. int vrl_ReadCFG(FILE *in)
  283.     {
  284.     char buff[256];
  285.     while (getline(buff, sizeof(buff), in))
  286.         vrl_ReadCFGProcessLine(buff);
  287.     return 0;
  288.     }
  289.  
  290. void vrl_ConfigSetCompassDisplay(vrl_Boolean flag)
  291.     {
  292.     show_compass = flag;
  293.     }
  294.  
  295. vrl_Boolean vrl_ConfigGetCompassDisplay(void)
  296.     {
  297.     return show_compass;
  298.     }
  299.  
  300. void vrl_ConfigToggleCompassDisplay(void)
  301.     {
  302.     show_compass = !show_compass;
  303.     }
  304.  
  305. void vrl_ConfigSetPositionDisplay(vrl_Boolean flag)
  306.     {
  307.     show_position = flag;
  308.     }
  309.  
  310. vrl_Boolean vrl_ConfigGetPositionDisplay(void)
  311.     {
  312.     return show_position;
  313.     }
  314.  
  315. void vrl_ConfigTogglePositionDisplay(void)
  316.     {
  317.     show_position = !show_position;
  318.     }
  319.  
  320. void vrl_ConfigSetFramerateDisplay(vrl_Boolean flag)
  321.     {
  322.     show_framerate = flag;
  323.     }
  324.  
  325. vrl_Boolean vrl_ConfigGetFramerateDisplay(void)
  326.     {
  327.     return show_framerate;
  328.     }
  329.  
  330. void vrl_ConfigToggleFramerateDisplay(void)
  331.     {
  332.     show_framerate = !show_framerate;
  333.     }
  334.  
  335. void vrl_ConfigStartup(char *filename)
  336.     {
  337.     vrl_SystemStartup();
  338.     vrl_ReadCFGfile(filename);
  339.     }
  340.  
  341. int vrl_ReadCFGfile(char *filename)
  342.     {
  343.     FILE *cfgfile;
  344.     cfgfile = fopen(vrl_FileFixupFilename(filename ? filename : "avril.cfg"), "r");
  345.     if (cfgfile)
  346.         {
  347.         vrl_ReadCFG(cfgfile);
  348.         fclose(cfgfile);
  349.         return 0;
  350.         }
  351.     return -1;
  352.     }
  353.